package com.vivo.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
@Service("PushAppMsgUtil")
public class PushAppMsgUtil {
	private static PushPayload payLoad;
	@Value("#{configProperties['masterSecret']}")
	private  String masterSecret;
	@Value("#{configProperties['AppKey']}")
	private  String appKey;
	public boolean pushMsgToUser(String userId,String msg){
		boolean flag=false;
		JPushClient jpushClient=new JPushClient(masterSecret.trim(), appKey.trim(), 3);
		payLoad=PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(userId)).setNotification(Notification.alert(msg)).build();
		try {
			PushResult result=jpushClient.sendPush(payLoad);
			System.out.println("push result:"+result+">>>>>");
			flag=result.isResultOK();
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			flag=false;
			//e.printStackTrace();
			
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			flag=false;
			//e.printStackTrace();
		}
		return flag;
	}
}
